using System;
using System.Collections.Generic;
using MvcCms.Data;

using MvcCms.Service.Cache;
using MvcCms.Service.Entities;
using MvcCms.Service.Validation;
using Raccoom.Xml;
using MvcCms.Service.Logging;
using MvcCms.Data.Entities;
using MvcCms.Service.Code;

namespace MvcCms.Service.CMSCode
{
    public class MiscService : IMiscService
    {
        #region Constructors

        private readonly ICacheService _cacheService;
        private readonly IMiscRepository _repository;
        private readonly IValidationDictionary _validationDictionary;

       
        public MiscService(IValidationDictionary validationDictionary,
                            IMiscRepository repository, ICacheService cacheService)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
            _cacheService = cacheService;
        }

        #endregion

        #region Validation Methods

        private bool ValidateLanguage(Language languageToValidate)
        {
            if (languageToValidate.LanguageName.Trim().Length == 0)
                _validationDictionary.AddError("LanguageName", "Language Name is required.");


            return _validationDictionary.IsValid;
        }

        #endregion

        #region IMiscService Members

        public IEnumerable<StateProvince> ListStates()
        {
            return _repository.ListStates();
        }

        public IEnumerable<Language> ListLanguages()
        {
            return _repository.ListLanguages();
        }
        public DTORowCount GetTableRowCount(string tableName, string socialMessageUserID)
        {
            return _repository.GetTableRowCount(tableName, socialMessageUserID, _cacheService.GetCurrentPortal().PortalID);
        }
        public Language GetLanguage(int langId)
        {
            return _repository.GetLanguage(langId);
        }

        public StateProvince GetStateProvince(int stateprovid)
        {
            return _repository.GetStateProvince(stateprovid);
        }
        public StateProvince GetStateProvince(string code)
        {
            return _repository.GetStateProvince(code);
        }
        public IEnumerable<DTORSSItem> GetRssItems(string rssurl)
        {
            RssChannel myRssChannel = _cacheService.GetRssFeed(rssurl);

            IList<DTORSSItem> mvccmsrssitems = new List<DTORSSItem>();

            foreach (RssItem rssitem in myRssChannel.Items)
            {
                var item = new DTORSSItem
                               {
                                   Author = rssitem.Author,
                                   Category = rssitem.Category,
                                   Comments = rssitem.Comments,
                                   Description = rssitem.Description,
                                   Enclosure = rssitem.Enclosure,
                                   Guid = rssitem.Guid,
                                   Link = rssitem.Link,
                                   PubDate = rssitem.PubDate,
                                   Source = rssitem.Source,
                                   Title = rssitem.Title,
                                   ElapsedTime = rssitem.PubDate.ToTimeSinceString()
                               };
                mvccmsrssitems.Add(item);
            }
            return mvccmsrssitems;
        }

        public bool CreateLanguage(Language languageToCreate)
        {
            // Validation logic
            if (!ValidateLanguage(languageToCreate))
                return false;

            // Database logic
            try
            {
                _repository.CreateLanguage(languageToCreate);
                _cacheService.ClearLanguages();
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }

        public bool EditLanguage(Language languageToEdit)
        {
            // Validation logic
            if (!ValidateLanguage(languageToEdit))
                return false;

            // Database logic
            try
            {
                _repository.EditLanguage(languageToEdit);
                _cacheService.ClearLanguages();
            }
            catch (Exception ex)
            {
                AppLogging.GetLogger().Error(GeneralMethods.GetStackNames(), ex);
                return false;
            }
            return true;
        }

        #endregion
    }
}